//
//  TMGHelpChildViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/11.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGHelpChildViewController.h"
#import "TMGHelpTableViewCell.h"
@interface TMGHelpChildViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSocue;
@property(nonatomic, strong) NSIndexPath     *slectedindexPath;
@end

@implementation TMGHelpChildViewController

- (instancetype)initWithTitle:(NSString *)title dataSouce:(NSArray *)dataSouce{
    if (self == [super init]) {
        self.dataSocue = [dataSouce mutableCopy];
        self.title = title;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"帮助中心";
    [self.tableView registerClass:[TMGHelpTableViewCell class] forCellReuseIdentifier:@"section0"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self configDataSouce];
    
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无问题" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendQuestionRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}
- (void)configDataSouce{

    [self.tableView reloadData];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.dataSocue count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        return 44;
    }
    TMGHelpModel * model = self.dataSocue[indexPath.row];
    TMGHelpTableViewCell * cell = (TMGHelpTableViewCell*)[self tableView:self.tableView cellForRowAtIndexPath:indexPath];
    cell.model = model;
    if (model.isSelected) {
        return model.cellHeight;
    }
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    

        TMGHelpTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"section0"];
        __weak typeof(self) weakSelf =  self;
        [cell.showButton setClicAction:^(UIButton *sender) {
            [weakSelf onSelectedShowButton:sender withIndexPath:indexPath];
        }];
        TMGHelpModel * model = self.dataSocue[indexPath.row];
        cell.model = model;
        return cell;

    
}
#pragma mark --Private Method
- (void)cancelSelctedIndexPath{
    if (self.slectedindexPath) {
        TMGHelpModel * model = self.dataSocue[self.slectedindexPath.row];
        model.isSelected = NO;
        [self.tableView reloadRowsAtIndexPaths:@[self.slectedindexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)onSelectedShowButton:(UIButton*)sender withIndexPath:(NSIndexPath*)indexPath{
    if (indexPath != self.slectedindexPath) {
        [self cancelSelctedIndexPath];
        self.slectedindexPath = indexPath;
        TMGHelpModel * model = self.dataSocue[indexPath.row];
        model.isSelected = ! model.isSelected ;
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }else{
        self.slectedindexPath = indexPath;
        TMGHelpModel * model = self.dataSocue[indexPath.row];
        model.isSelected = ! model.isSelected ;
        [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}
#pragma mark --NetWork Method
- (void)sendQuestionRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.questionID.length) {
        [dic setObject:self.questionID forKey:@"id"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/usuallyQuestion"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                  NSArray * array = responseObject[@"data"];
                                                 self.dataSocue = [NSMutableArray new];
                                                 for (NSDictionary *modelDic in array) {
                                                     TMGHelpModel * model = [TMGHelpModel mj_objectWithKeyValues:modelDic];
                                                     [self.dataSocue addObject:model];
                                                 }
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
